package com.gjy.concurrency.aotmic;

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @author 宫静雨
 * @version v1.0
 * @since 2022-11-14 20:07:22
 */
public class Atomic1Test {

    @Test
    public void test1() {
        new AtomicStampedLock().a();
    }

    private static final class AtomicStampedLock {
        private final AtomicStampedReference<Integer> atomic =
                new AtomicStampedReference<>(0, 1);

        public void a() {
            atomic.compareAndSet(0, 10, 1, 10);

            System.out.println("atomic.getReference() = " + atomic.getReference());
            System.out.println("atomic.getStamp() = " + atomic.getStamp());
        }
    }

    private static final class OptimisticLock {
        private final AtomicMarkableReference<Integer> atomic = new AtomicMarkableReference<>(0, true);

        public void get() {
            System.out.println(atomic);
        }

    }

    private static final class Counter {
        private final AtomicInteger ATOMIC_INTEGER;

        public Counter(Integer initial) {
            ATOMIC_INTEGER = new AtomicInteger();
        }

        public void incr() {
            ATOMIC_INTEGER.incrementAndGet();
        }

        public void decr() {
            ATOMIC_INTEGER.getAndDecrement();
        }

    }
}
